package com.atguigu.gmall.pay.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.request.AlipayTradeQueryRequest;
import com.alipay.api.response.AlipayTradeQueryResponse;
import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.common.util.DateUtil;
import com.atguigu.gmall.common.util.JsonUtil;
import com.atguigu.gmall.feign.order.OrderFeignClient;
import com.atguigu.gmall.model.order.OrderInfo;
import com.atguigu.gmall.pay.config.AlipayProperties;
import com.atguigu.gmall.pay.service.PayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author A
 * @version 1.0
 * @description: TODO
 * @date 2024/10/14 6:03
 */
@Service
public class PayServiceImpl implements PayService {
    @Autowired
    AlipayClient alipayClient;
    @Autowired
    AlipayProperties properties;
    @Autowired
    OrderFeignClient orderFeignClient;
    @Override
    public String submit(Long orderId) throws AlipayApiException {
        //1.创建一个客户端

        //2.准备一个支付请求
        AlipayTradePagePayRequest payRequest = new AlipayTradePagePayRequest();
        //3.设置好请求参数
        payRequest.setNotifyUrl(properties.getNotify_url());
        payRequest.setReturnUrl(properties.getReturn_url());
        String json=buildBizContent(orderId);
        payRequest.setBizContent(json);
        //执行请求
        String body = alipayClient.pageExecute(payRequest).getBody();
        return body;
    }

    @Override
    public String getOrderStatus(String outTradeNo) throws AlipayApiException {
        // 构造请求参数以调用接口
        AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
        JSONObject bizContent = new JSONObject();
        bizContent.put("out_trade_no", outTradeNo);
        request.setBizContent(bizContent.toString());
        AlipayTradeQueryResponse response = alipayClient.execute(request);
        String tradeStatus = null;
        if (response.isSuccess()) {
            tradeStatus = response.getTradeStatus();
        }
        return tradeStatus;
    }

    private String buildBizContent(Long orderId) {

        Map<String, String> map = new HashMap<>();
        Result<OrderInfo> info = orderFeignClient.getOrderInfoById(orderId);
        if (info.isOk()){
            OrderInfo orderInfo = info.getData();
            map.put("out_trade_no",orderInfo.getOutTradeNo());
            map.put("total_amount",orderInfo.getTotalAmount().toString());
            map.put("subject",orderInfo.getTradeBody());
            map.put("product_code","FAST_INSTANT_TRADE_PAY");
            String date = DateUtil.formatDate(orderInfo.getExpireTime(), "yyyy-MM-dd HH:mm:ss");
            map.put("time_expire",date);
        }

        return JsonUtil.obj2Json(map);
    }
}
